package dk.mirasola.systemtraining.server;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class MailUtil {
    private static final String NOTIFICATION_FROM = "systemtrainingnotifier@mirasola.dk";
    private static final String NOTIFICATION_NO_REPLY_EMAIL = "no-reply@mirasola.dk";

    public static void sendNotificationMail(String to, String subject, String body) {
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        try {
            Message msg = new MimeMessage(session);
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            msg.setFrom(new InternetAddress(NOTIFICATION_FROM));
            msg.setReplyTo(new InternetAddress[]{new InternetAddress(NOTIFICATION_NO_REPLY_EMAIL)});
            msg.setSubject(subject);
            msg.setText(body);
            Transport.send(msg);
        } catch (AddressException e) {
            throw new RuntimeException(e);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}
